package com.csw;

import jdk.nashorn.internal.ir.CallNode;

import java.awt.*;

/**
 * @author A
 * @version 1.0
 * @description: 底层地图
 * 绘制游戏相关组件
 * @date 2022/11/22 22:36
 */
public class MapBottom {

    BottomRay bottomRay=new BottomRay();

    BottomNum bottomNum=new BottomNum();
    {
        bottomRay.newRay();
        bottomNum.newBottomNum();
    }


    void paintSelf(Graphics g){

        g.setColor(Color.red);
        //画线
        for (int i = 0; i < GameUtil.Map_W; i++) {
            g.drawLine(GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,
                    3*GameUtil.OFFSET,
                    GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,
                    3*GameUtil.OFFSET+GameUtil.Map_H*GameUtil.SQUARE_LENGTH);
        }
        for (int i = 0; i < GameUtil.Map_H; i++) {
            g.drawLine(GameUtil.OFFSET,
                    3*GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH,
                    GameUtil.OFFSET+GameUtil.Map_W*GameUtil.SQUARE_LENGTH,
                    3*GameUtil.OFFSET+i*GameUtil.SQUARE_LENGTH);
        }


        //画雷
        for (int i = 1; i <= GameUtil.Map_W; i++) {
            for (int j = 1; j <= GameUtil.Map_H; j++) {
                //画雷
                if (GameUtil.DATA_BOTTOM[i][j]==-1){
                    g.drawImage(GameUtil.mine,
                            GameUtil.OFFSET + (i - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.OFFSET * 3 + (j - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.SQUARE_LENGTH - 2,
                            GameUtil.SQUARE_LENGTH - 2,
                            null);
                }
                //画数字
                if (GameUtil.DATA_BOTTOM[i][j]>=0){
                    g.drawImage(GameUtil.images[GameUtil.DATA_BOTTOM[i][j]],
                            GameUtil.OFFSET + (i - 1) * GameUtil.SQUARE_LENGTH + 15,
                            GameUtil.OFFSET * 3 + (j - 1) * GameUtil.SQUARE_LENGTH + 5,

                            null);
                }
            }


        }

        switch (GameUtil.state){
            //0:游戏中 1:游戏胜利 2:游戏结束
            case 0:
                g.drawImage(GameUtil.question,
                        GameUtil.OFFSET + GameUtil.SQUARE_LENGTH * (GameUtil.Map_W / 2),
                        GameUtil.OFFSET,
                        null);
                GameUtil.endTime=System.currentTimeMillis();
            break;
            case 1:
                g.drawImage(GameUtil.smile,
                        GameUtil.OFFSET + GameUtil.SQUARE_LENGTH * (GameUtil.Map_W / 2),
                        GameUtil.OFFSET,
                        null);
            break;
            case 2:
                g.drawImage(GameUtil.fail,
                        GameUtil.OFFSET + GameUtil.SQUARE_LENGTH * (GameUtil.Map_W / 2),
                        GameUtil.OFFSET,
                        null);
            break;
            default:
        }

        GameUtil.drawStr(g, GameUtil.OFFSET, 2*GameUtil.OFFSET, 30, Color.red, ""+(GameUtil.RAY_MAX-GameUtil.flagCount));
        GameUtil.drawStr(g, GameUtil.OFFSET+GameUtil.Map_W*(GameUtil.SQUARE_LENGTH-1), 2*GameUtil.OFFSET, 30, Color.red, ""+(GameUtil.endTime-GameUtil.startTime)/1000);
    }
    //重置游戏
    void regame(){
        for (int i=1;i<=GameUtil.Map_W;i++){
            for (int j=1;j<=GameUtil.Map_H;j++){
                //将所有格子重置为没有雷的状态
                GameUtil.DATA_BOTTOM[i][j]=0;
            }
        }
        //需要新生成雷和数字
        bottomRay.newRay();
        bottomNum.newBottomNum();
    }
}
